#include "formbuilder.h"
#include <QBitmap>
#include <QPainter>
#include <QPlainTextEdit>
#include "styledlabel.h"
#include <QLineEdit>
#include <QComboBox>
#include <QListWidget>

FormBuilder::FormBuilder(QObject *parent) :
    QObject(parent)
{
    m_styled = "QEventLabel {"
            "border-style: solid;"
            "border-width: 1px;"
            "border-radius: 6px;"
            "min-width: 1em;"
            "border-color: darkgray;"
            "background-color: white;"
            "}";
}

QEventLabel *FormBuilder::driverForm()
{
    if (m_installDriverForm)
        return m_installDriverForm.data();

    m_installDriverForm.reset(buildBase(160));
    StyledLabel *lbl = new StyledLabel(m_installDriverForm.data());
    StyledLabel *question = new StyledLabel(lbl);
    lbl->move(16, 40);
    lbl->resize(308, 80);
    question->move(8, 15);
    QPixmap      questionP(":/images/images/question.png");
    QPixmap      questionM(":/images/images/question-mask.png");
    question->setPixmap(questionP);
    question->setMask(questionM);
    QPlainTextEdit *edit = new QPlainTextEdit(lbl);
    QFont font(edit->font());
    font.setPixelSize(13);
    edit->setFont(font);
    edit->setPlainText(QObject::tr("Для корректной работы программы требуется установить Tap-Win32 драйвер. Установить?"));
    edit->setFrameStyle(0);
    edit->move(60, 5);
    edit->setEnabled(false);
    edit->resize(240, 68);
    m_installDriverForm->m_message = edit;
    QEventLabel * yes = new QEventLabel(m_installDriverForm.data());
    yes->setText(tr("Да"));
    QFont yFont(yes->font());
    yFont.setPixelSize(13);
    yFont.setUnderline(true);
    yes->setFont(yFont);
    yes->move(24, 128);
    yes->setCheckClick(true);
    yes->setCheckMove(false);
    connect(yes, SIGNAL(onClick()), m_installDriverForm.data(), SLOT(close()));
    QEventLabel * no = new QEventLabel(m_installDriverForm.data());
    QFont nFont(no->font());
    nFont.setPixelSize(13);
    nFont.setUnderline(true);
    no->setFont(nFont);
    no->setText(tr("Нет"));
    no->move(294, 128);
    no->setCheckClick(true);
    no->setCheckMove(false);
    connect(no, SIGNAL(onClick()), m_installDriverForm.data(), SLOT(close()));
    return m_installDriverForm.data();
}

QEventLabel *FormBuilder::loginForm()
{
    if (m_loginForm)
        return m_loginForm.data();

    m_loginForm.reset(buildBase(176));
    QEventLabel *cross = new QEventLabel(m_loginForm.data());
    cross->move(312, 12);
    QPixmap      crossP(":/images/images/cross.png");
    QPixmap      crossM(":/images/images/cross-mask.png");
    cross->setPixmap(crossP);
    cross->setMask(crossM);
    cross->setCheckClick(true);
    cross->setCheckMove(false);
    connect(cross, SIGNAL(onClick()), m_loginForm.data(), SLOT(close()));

    QLabel *back = new QLabel(m_loginForm.data());
    back->move(0, 32);
    QPixmap      backP(":/images/images/back.png");
    back->setPixmap(backP);


    QEventLabel * regsLink = new QEventLabel(m_loginForm.data());
    regsLink->setText(tr("Регистрация"));
    QFont yFont(regsLink->font());
    yFont.setPixelSize(13);
    yFont.setUnderline(true);
    regsLink->setFont(yFont);
    regsLink->move(12, 138);
    regsLink->setCheckClick(true);
    regsLink->setCheckMove(false);
    connect(regsLink, SIGNAL(onClick()), m_loginForm.data(), SLOT(close()));
    QEventLabel * forgotLink = new QEventLabel(m_loginForm.data());
    QFont nFont(forgotLink->font());
    nFont.setPixelSize(13);
    nFont.setUnderline(true);
    forgotLink->setFont(nFont);
    forgotLink->setText(tr("Забыли пароль?"));
    forgotLink->move(120, 138);
    forgotLink->setCheckClick(true);
    forgotLink->setCheckMove(false);
    connect(forgotLink, SIGNAL(onClick()), m_loginForm.data(), SLOT(close()));

    QEventLabel *more = new QEventLabel(m_loginForm.data());
    more->move(300, 136);
    QPixmap      moreP(":/images/images/more.png");
    QPixmap      moreM(":/images/images/more-mask.png");
    more->setPixmap(moreP);
    more->setMask(moreM);
    more->setCheckClick(true);
    more->setCheckMove(false);
    connect(more, SIGNAL(onClick()), m_loginForm.data(), SLOT(close()));
    StyledLabel *login = new StyledLabel(m_loginForm.data());
    login->move(14, 46);
    login->resize(150, 25);
    QLineEdit *editLogin = new QLineEdit(tr("Логин"), login);
    editLogin->move(5, 1);
    editLogin->setFrame(false);
    editLogin->resize(140, 22);
    StyledLabel *password = new StyledLabel(m_loginForm.data());
    password->move(172, 46);
    password->resize(150, 25);
    QLineEdit *passLogin = new QLineEdit(tr("Пароль"), password);
    passLogin->move(5, 1);
    passLogin->setFrame(false);
    passLogin->setEchoMode(QLineEdit::Password);
    passLogin->resize(120, 22);
    QEventLabel *lock = new QEventLabel(password);
    lock->move(130, 4);
    QPixmap      lockP(":/images/images/lock.png");
    QPixmap      lockM(":/images/images/lock-mask.png");
    lock->setPixmap(lockP);
    lock->setMask(lockM);
    lock->setCheckClick(true);
    lock->setCheckMove(false);
    lock->setVisible(false);
    QEventLabel *eye = new QEventLabel(password);
    eye->move(125, 4);
    QPixmap      eyeP(":/images/images/eye.png");
    QPixmap      eyeM(":/images/images/eye-mask.png");
    eye->setPixmap(eyeP);
    eye->setMask(eyeM);
    eye->setCheckClick(true);
    eye->setCheckMove(false);

    QEventLabel *checkB = new QEventLabel(m_loginForm.data());
    checkB->setStyleSheet(m_styled);
    checkB->move(174, 86);
    checkB->resize(16, 16);
    checkB->setCheckClick(true);
    checkB->setCheckMove(false);
    QEventLabel *check = new QEventLabel(checkB);
    QPixmap      checkP(":/images/images/check.png");
    QPixmap      checkM(":/images/images/check-mask.png");
    check->setPixmap(checkP);
    check->setMask(checkM);
    check->move(2, 2);
    check->setCheckClick(true);
    check->setCheckMove(false);

    QLabel * savePassLabel = new QLabel(m_loginForm.data());
    savePassLabel->setText(tr("Сохранить пароль"));
    QFont sFont(savePassLabel->font());
    savePassLabel->setStyleSheet("QLabel {color: white }");
    sFont.setPixelSize(13);
    savePassLabel->setFont(sFont);
    savePassLabel->move(198, 86);

    QComboBox *box = new QComboBox(m_loginForm.data());
    box->move(14, 84);
    box->resize(150, 24);
    box->setStyleSheet(" QComboBox {"
                       "padding: 1px 18px 1px 13px;"
                       "border: 1px solid gray;"
                       "border-radius: 5px;}"
                       "QComboBox::drop-down {"
                       "width: 20px;"
                       "border-left-width: 1px;"
                       "border-left-color: darkgray;"
                       "border-left-style: solid;"
                       "border-top-right-radius: 5px;"
                       "border-bottom-right-radius: 5px;}"
                       "QComboBox::down-arrow {"
                       "image: url(:/images/images/drop.png);}"
                       );
    box->addItem(QIcon(":/images/images/russian.png"), tr("Русский"));




    return m_loginForm.data();
}

QEventLabel *FormBuilder::messageForm()
{
    if (m_msgForm)
        return m_msgForm.data();
    m_msgForm.reset(buildBase(120));
    QEventLabel *cross = new QEventLabel(m_msgForm.data());
    cross->move(312, 12);
    QPixmap      crossP(":/images/images/cross.png");
    QPixmap      crossM(":/images/images/cross-mask.png");
    cross->setPixmap(crossP);
    cross->setMask(crossM);
    cross->setCheckClick(true);
    cross->setCheckMove(false);
    connect(cross, SIGNAL(onClick()), m_msgForm.data(), SLOT(close()));
    StyledLabel *lbl = new StyledLabel(m_msgForm.data());
    StyledLabel *mark = new StyledLabel(lbl);
    lbl->move(16, 40);
    lbl->resize(308, 60);
    mark->move(8, 8);
    QPixmap      markP(":/images/images/mark.png");
    QPixmap      markM(":/images/images/mark-mask.png");
    mark->setPixmap(markP);
    mark->setMask(markM);
    QPlainTextEdit *edit = new QPlainTextEdit((lbl));
    QFont font(edit->font());
    font.setPixelSize(13);
    edit->setFont(font);
    edit->setPlainText(QObject::tr("Неверный Логин и Пароль"));
    edit->setFrameStyle(0);
    edit->move(74, 15);
    edit->setEnabled(false);
    edit->resize(220, 28);
    m_msgForm->m_message = edit;
    return m_msgForm.data();
}

QEventLabel *FormBuilder::main()
{
    if (m_main)
        return m_main.data();
    m_main.reset(buildBase(342));
    QEventLabel *cross = new QEventLabel(m_main.data());
    cross->move(312, 12);
    QPixmap      crossP(":/images/images/cross.png");
    QPixmap      crossM(":/images/images/cross-mask.png");
    cross->setPixmap(crossP);
    cross->setMask(crossM);
    cross->setCheckClick(true);
    cross->setCheckMove(false);
    connect(cross, SIGNAL(onClick()), m_main.data(), SLOT(close()));
    QEventLabel *down = new QEventLabel(m_main.data());
    down->move(292, 15);
    QPixmap      downP(":/images/images/down.png");
    QPixmap      downM(":/images/images/down-mask.png");
    down->setPixmap(downP);
    down->setMask(downM);
    down->setCheckMove(false);

    QLabel *back = new QLabel(m_main.data());
    back->move(0, 32);
    QPixmap      backP(":/images/images/back.png");
    backP = backP.scaled(backP.width(), backP.height()*1.6);
    back->setPixmap(backP);

    QLabel *mid = new QLabel(m_main.data());
    mid->move(0, 32 + backP.height()/3);
    QPixmap      midP(":/images/images/line.png");
    QPixmap      midM(":/images/images/line-mask.png");
    mid->setPixmap(midP);
    mid->setMask(midM);
    QEventLabel *mid2 = new QEventLabel(m_main.data());
    mid2->move(0, 32 + 2*backP.height()/3);
    mid2->setPixmap(midP);
    mid2->setMask(midM);

    QLabel *ip = new QLabel(m_main.data());
    ip->move(16, 42);
    QPixmap      ipP(":/images/images/ip.png");
    QPixmap      ipM(":/images/images/ip-mask.png");
    ip->setPixmap(ipP);
    ip->setMask(ipM);
    QLabel *manage = new QLabel(m_main.data());
    manage->move(16, 144);
    QPixmap      manageP(":/images/images/manage.png");
    QPixmap      manageM(":/images/images/manage-mask.png");
    manage->setPixmap(manageP);
    manage->setMask(manageM);
    QLabel *settings = new QLabel(m_main.data());
    settings->move(16, 90);
    QPixmap      settingsP(":/images/images/settings.png");
    QPixmap      settingsM(":/images/images/settings-mask.png");
    settings->setPixmap(settingsP);
    settings->setMask(settingsM);

    QLabel * ipLabel = new QLabel(m_main.data());
    ipLabel->setText(tr("192.168.111.211"));
    QFont sFont(ipLabel->font());
    ipLabel->setStyleSheet("QLabel {color: lightgray }");
    sFont.setPixelSize(16);
    ipLabel->setFont(sFont);
    ipLabel->move(60, 50);
    QLabel * settingsLabel = new QLabel(m_main.data());
    settingsLabel->setText(tr("Настройки"));
    settingsLabel->setStyleSheet("QLabel {color: white }");
    settingsLabel->setFont(sFont);
    settingsLabel->move(60, 98);
    QLabel * manageLabel = new QLabel(m_main.data());
    manageLabel->setText(tr("Управление"));
    manageLabel->setStyleSheet("QLabel {color: lightgray }");
    manageLabel->setFont(sFont);
    manageLabel->move(60, 152);

    StyledLabel *widBox = new StyledLabel(m_main.data());
    widBox->move(10, 216);
    widBox->resize(320, 75);
    QListWidget *wid = new QListWidget(m_main.data());
    wid->setFrameStyle(0);
    wid->move(16, 220);
    wid->resize(310, 65);
    /*QListItem *it = addListItem(QPixmap(":/images/images/british.png"),
                                QString("British IP"), QString("ololol"));
    QListWidgetItem *item = new QListWidgetItem();
    item->setSizeHint(QSize(0,65));
    wid->addItem(item);
    wid->setItemWidget(item,it);//*/

    QEventLabel *refresh = new QEventLabel(m_main.data());
    refresh->move(308, 187);
    QPixmap      refreshP(":/images/images/refresh.png");
    QPixmap      refreshM(":/images/images/refresh-mask.png");
    refresh->setPixmap(refreshP);
    refresh->setMask(refreshM);
    refresh->setCheckClick(true);
    refresh->setCheckMove(false);
    QEventLabel *search = new QEventLabel(m_main.data());
    search->move(276, 189);
    QPixmap      searchP(":/images/images/search.png");
    QPixmap      searchM(":/images/images/search-mask.png");
    search->setPixmap(searchP);
    search->setMask(searchM);
    search->setCheckClick(true);
    search->setCheckMove(false);
    QEventLabel *less = new QEventLabel(m_main.data());
    less->move(10, 300);
    QPixmap      lessP(":/images/images/less.png");
    QPixmap      lessM(":/images/images/less-mask.png");
    less->setPixmap(lessP);
    less->setMask(lessM);
    less->setCheckClick(true);
    less->setCheckMove(false);
    QEventLabel *globe = new QEventLabel(m_main.data());
    globe->move(46, 300);
    QPixmap      globeP(":/images/images/globe.png");
    QPixmap      globeM(":/images/images/globe-mask.png");
    globe->setPixmap(globeP);
    globe->setMask(globeM);
    globe->setCheckClick(true);
    globe->setCheckMove(false);
    QEventLabel *letter = new QEventLabel(m_main.data());
    letter->move(294, 305);
    QPixmap      letterP(":/images/images/letter.png");
    letter->setPixmap(letterP);
    letter->setCheckClick(true);
    letter->setCheckMove(false);
    QEventLabel *btn = new QEventLabel(m_main.data());
    btn->move(10, 190);
    QPixmap      btnP(":/images/images/btn.png");
    QPixmap      btnM(":/images/images/btn-mask.png");
    btn->setPixmap(btnP);
    btn->setMask(btnM);
    btn->setCheckClick(true);
    btn->setCheckMove(false);

    return m_main.data();
}

QEventLabel *FormBuilder::logForm()
{
    if (m_log)
        return m_log.data();

    m_log.reset(buildBase(460, true));
    QEventLabel *cross = new QEventLabel(m_log.data());
    cross->move(550, 12);
    QPixmap      crossP(":/images/images/cross.png");
    QPixmap      crossM(":/images/images/cross-mask.png");
    cross->setPixmap(crossP);
    cross->setMask(crossM);
    cross->setCheckClick(true);
    cross->setCheckMove(false);
    connect(cross, SIGNAL(onClick()), m_log.data(), SLOT(close()));
    StyledLabel *lbl = new StyledLabel(m_log.data());
    lbl->move(16, 40);
    lbl->resize(550, 380);
    QPlainTextEdit *edit = new QPlainTextEdit(lbl);
    QFont font(edit->font());
    font.setPixelSize(13);
    edit->setFont(font);
    edit->setPlainText(QObject::tr("No Logs"));
    edit->setFrameStyle(0);
    edit->move(10, 5);
    edit->setEnabled(false);
    edit->resize(532, 362);
    m_log->m_message = edit;
    QEventLabel * yes = new QEventLabel(m_log.data());
    yes->setText(tr("Отключить"));
    QFont yFont(yes->font());
    yFont.setPixelSize(13);
    yFont.setUnderline(true);
    yes->setFont(yFont);
    yes->move(24, 428);
    yes->setCheckClick(true);
    yes->setCheckMove(false);
    QEventLabel * no = new QEventLabel(m_log.data());
    QFont nFont(no->font());
    nFont.setPixelSize(13);
    nFont.setUnderline(true);
    no->setFont(nFont);
    no->setText(tr("Переподключить"));
    no->move(100, 428);
    no->setCheckClick(true);
    no->setCheckMove(false);
    QEventLabel *letter = new QEventLabel(m_log.data());
    letter->move(540, 424);
    QPixmap      letterP(":/images/images/letter.png");
    letter->setPixmap(letterP);
    letter->setCheckClick(true);
    letter->setCheckMove(false);
    return m_log.data();
}

QEventLabel *FormBuilder::sendForm()
{
}

QListItem *FormBuilder::addListItem(QPixmap &pic, QString& textA, QString& textB, int status)
{
    QListItem *itm = new QListItem(pic, textA, textB, status);
    return itm;
}

QEventLabel *FormBuilder::buildBase(int nHeight, bool big)
{
    QEventLabel* lbl = new QEventLabel(0, Qt::FramelessWindowHint | Qt::Window);

    QPixmap      top;
    QPixmap      tMask;
    QPixmap      bottom;
    QPixmap      bMask;
    if (big) {
        top.load(":/images/images/topBig.png");
        tMask.load(":/images/images/topBig-mask.png");
        bottom.load(":/images/images/bottomBig.png");
        bMask.load(":/images/images/bottomBig-mask.png");
    } else {
        top.load(":/images/images/top.png");
        tMask.load(":/images/images/top-mask.png");
        bottom.load(":/images/images/bottom.png");
        bMask.load(":/images/images/bottom-mask.png");
    }
    int nwidth = top.width();
    QPixmap toDraw(nwidth, nHeight);
    int hei = nHeight - top.height() - bottom.height();
    QPainter p(&toDraw);
    p.drawPixmap(QPoint(0, 0), top, top.rect());
    QColor formPen = QColor(230, 230, 230);
    p.setPen(formPen);
    p.setBrush(formPen);
    QPoint left = top.rect().bottomLeft();
    QPoint right = top.rect().bottomRight();
    p.drawRect(QRect(left, left + QPoint(nwidth, hei)));
    p.setPen(Qt::white);
    p.drawLine(left, left + QPoint(0, hei));
    p.drawLine(right, right + QPoint(0, hei));
    p.setBrush(Qt::white);
    QPixmap toMask(nwidth, nHeight);
    toMask.fill();
    QPainter x(&toMask);
    x.drawPixmap(QPoint(0, 0), tMask, tMask.rect());
    x.setBrush(Qt::black);
    x.drawRect(QRect(left, left + QPoint(top.width(), hei)));

    p.drawPixmap(QPoint(0, hei)+left, bottom, bottom.rect());
    p.end();

    x.drawPixmap(QPoint(0, hei)+left, bMask, bMask.rect());
    x.end();

    lbl->setPixmap(toDraw);
    lbl->setMask(toMask);
    lbl->resize(nwidth, nHeight);

    return lbl;
}


QListItem::QListItem(QPixmap &pic, QString &textA, QString &textB, int status, QWidget *parent) :
    QWidget(parent)
{
    m_pix = new QLabel(this);
    m_pix->setPixmap(pic);
    m_pix->move(10, 15);
    m_A = new QLabel(this);
    m_A->setText(textA);
    m_A->move(50, 15);
    QFont sFont(m_A->font());
    sFont.setPixelSize(13);
    m_A->setFont(sFont);
    m_B = new QLabel(this);
    m_B->setText(textB);
    m_B->setStyleSheet("QLabel {color: darkgray }");
    m_B->move(50, 28);
    if (!status)
        m_stat = 0;
    else {//TODO:
    }
}

QListItem::~QListItem()
{
    if (m_pix)
        delete m_pix;
    if (m_A)
        delete m_A;
    if (m_B)
        delete m_B;
    if (m_stat)
        delete m_stat;
}
